#include "mymodel.h"

MyModel::MyModel(QObject *parent) : QAbstractTableModel(parent)
{

}

MyModel::~MyModel()
{

}

int MyModel::rowCount(const QModelIndex &parent) const
{
    if(m_table_type == TableType_BMS208)
    {
        return m_BMS208ModelData.count();
    }
    return 0;
}

int MyModel::columnCount(const QModelIndex &parent) const
{
    return headeList.count();
}

QVariant MyModel::data(const QModelIndex &index, int role) const
{
    int row = index.row();
    int col = index.column();

    if(!index.isValid())
    {
        return QVariant();
    }

    if(role == Qt::TextAlignmentRole)
    {
        return Qt::AlignCenter; //剧中对齐
    }
    else if(role == Qt::DisplayRole || role == Qt::EditRole)
    {
        if(m_table_type == TableType_BMS208)
        {
            switch (index.column())
            {
            case 0:return m_BMS208ModelData.at(row).name;
            case 1:return m_BMS208ModelData.at(row).value;
            case 2:return m_BMS208ModelData.at(row).description;
            }
        }
    }
    else if(role == Qt::UserRole)
    {        
        if(m_table_type == TableType_BMS208)
        {
            return QVariant::fromValue<BMS_Normal>(m_BMS208ModelData[row]);
        }
    }

    return QVariant();
}

QVariant MyModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role == Qt::DisplayRole && orientation == Qt::Horizontal)
    {
        if (section < headeList.size())
        {
            return headeList[section];
        }
    }
    return QAbstractItemModel::headerData(section, orientation, role);
}

bool MyModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (!index.isValid())
        return false;

    if (role == Qt::DisplayRole || role == Qt::EditRole)
    {
        const int row = index.row();

        if(m_table_type == TableType_BMS208)
        {
            switch (index.column())
            {
            case 0:m_BMS208ModelData[row].name = value.toString();break;
            case 1:m_BMS208ModelData[row].value = value.toInt();break;
            case 2:m_BMS208ModelData[row].description = value.toString();break;
            }
        }
    }

    return false;
}

bool MyModel::removeRows(int row, int count, const QModelIndex &parent)
{
    if (row < 0 || count<1 || row + count>rowCount())
        return false;
    //需要将操作放到beginRemoveRows和endRemoveRows两个函数调用之间

    beginRemoveRows(parent, row, row + count - 1);

    //TODO：这里需要注意删除，没用到暂时不处理    
    if (m_DeleteList.size() != 0)
    {
        if(m_table_type == TableType_BMS208)
        {
            for (int i = 0; i < m_DeleteList.size(); i++)
            {
                m_BMS208ModelData.removeAt(m_DeleteList[i]);
            }
        }
    }

    m_DeleteList.clear();
    endRemoveRows();

    return true;
}

void MyModel::SetCurrentTableTyoe(TableType type)
{
    m_table_type = type;
}

void MyModel::SetDeleteList(QList<int> i_List)
{
    m_DeleteList = i_List;
}

void MyModel::SetHeadData(QStringList i_list)
{
    headeList = i_list;
}

void MyModel::SetModelData(QList<BMS_Normal> model)
{
    beginResetModel();
    m_BMS208ModelData = model;
    endResetModel();
}
